package service

import (
	"context"
	"errors"
	"github.com/kuangshp/go-utils/k"
	"github.com/spf13/cast"
	"gorm.io/gorm"
	"insighttracking/common/utils"
	"insighttracking/query/bo"
	"insighttracking/query/dao"
	"insighttracking/query/model"
	"strings"
)

type _warehouseService struct{}

func (s _warehouseService) CreateApi(ctx context.Context, req bo.CreateWarehouseBo) error {
	_, err := dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.EnterpriseID.Eq(req.EnterpriseId), dao.WarehouseEntity.Name.Eq(req.Name)).First()
	if !errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("名字已经存在,不能重复")
	}
	if req.IsDefault == 1 {
		warehouseEntity, err := dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.EnterpriseID.Eq(req.EnterpriseId), dao.WarehouseEntity.IsDefault.Eq(1)).First()
		if !errors.Is(err, gorm.ErrRecordNotFound) {
			dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.ID.Eq(warehouseEntity.ID)).UpdateSimple(
				dao.WarehouseEntity.IsDefault.Value(0),
			)
			//return errors.New("已经存在默认仓库,不能继续设置默认仓库")
		}
	}
	if err = dao.WarehouseEntity.WithContext(ctx).Create(&model.WarehouseEntity{
		EnterpriseID: req.EnterpriseId, // 关联到enterprise表主键id
		Name:         req.Name,         // 仓库名称
		Code:         req.Code,         // 仓库编码
		Status:       req.Status,       // 状态0表示正常,1表示禁用
		IsDefault:    req.IsDefault,    // 是否为默认仓库,0表示不是，1表示是
		PersonID:     req.PersonID,     // 仓库负责人,关联到enterprise_person表主键id
		Describe:     req.Describe,     // 仓库名称描述
	}); err != nil {
		return errors.New("创建失败")
	}
	return nil
}

func (s _warehouseService) DeleteByIdApi(ctx context.Context, enterpriseId int64, id int64) error {
	if _, err := dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.EnterpriseID.Eq(enterpriseId), dao.WarehouseEntity.ID.Eq(id)).Delete(); err != nil {
		return err
	}
	return nil
}

func (s _warehouseService) GetWarehouseListApi(ctx context.Context, enterpriseId int64) ([]*model.WarehouseEntity, error) {
	return dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.EnterpriseID.Eq(enterpriseId), dao.WarehouseEntity.Status.Eq(0)).Order(dao.WarehouseEntity.ID.Desc()).Find()
}

func (s _warehouseService) GetWarehousePageApi(ctx context.Context, req bo.WarehousePageReqBo) (warehouseBo []bo.WarehouseBo, count int64, err error) {
	tx := dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.EnterpriseID.Eq(req.EnterpriseId))
	if req.Name != "" {
		req.Name = strings.ReplaceAll(req.Name, "%", "\\%")
		req.Name = strings.ReplaceAll(req.Name, "_", "\\%")
		tx = tx.Where(dao.WarehouseEntity.Name.Like("%" + req.Name + "%")).Or(dao.WarehouseEntity.Code.Like("%" + req.Name + "%"))
	}
	if req.Status != "" {
		tx = tx.Where(dao.WarehouseEntity.Status.Eq(cast.ToInt64(req.Status)))
	}
	offset, limit := utils.DbPage(req.PageNumber, req.PageSize)
	
	warehouseBo = make([]bo.WarehouseBo, 0)
	count, err = tx.
		Select(
			dao.WarehouseEntity.ALL,
			dao.EnterprisePersonEntity.Name.As("PersonName"),
		).
		LeftJoin(dao.EnterprisePersonEntity, dao.EnterprisePersonEntity.ID.EqCol(dao.WarehouseEntity.PersonID)).
		Order(dao.WarehouseEntity.ID.Desc()).
		ScanByPage(&warehouseBo, offset, limit)
	return warehouseBo, count, err
}

func (s _warehouseService) ModifyStatusByIdApi(ctx context.Context, id int64) error {
	warehouseEntity, err := dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.ID.Eq(id)).First()
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("传递的id错误")
	}
	if _, err = dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.ID.Eq(id)).UpdateSimple(
		dao.WarehouseEntity.Status.Value(int64(k.If(warehouseEntity.Status == 0, 1, 0))),
	); err != nil {
		return errors.New("修改失败")
	}
	return nil
}

func (s _warehouseService) ModifyByIdApi(ctx context.Context, id int64, req bo.CreateWarehouseBo) error {
	warehouseEntity, err := dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.EnterpriseID.Eq(req.EnterpriseId), dao.WarehouseEntity.Name.Eq(req.Name)).First()
	if !errors.Is(err, gorm.ErrRecordNotFound) && id != warehouseEntity.ID {
		return errors.New("名字已经存在,不能重复")
	}
	if req.IsDefault == 1 {
		warehouseEntity1, err := dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.EnterpriseID.Eq(req.EnterpriseId), dao.WarehouseEntity.IsDefault.Eq(1)).First()
		if !errors.Is(err, gorm.ErrRecordNotFound) {
			if _, err = dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.ID.Eq(warehouseEntity1.ID)).UpdateSimple(
				dao.WarehouseEntity.IsDefault.Value(0),
			); err != nil {
				return errors.New("修改失败")
			}
		}
	}
	if _, err = dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.ID.Eq(id)).UpdateSimple(
		dao.WarehouseEntity.EnterpriseID.Value(req.EnterpriseId), // 关联到enterprise表主键id
		dao.WarehouseEntity.Name.Value(req.Name),                 // 仓库名称
		dao.WarehouseEntity.Code.Value(req.Code),                 // 仓库编码
		dao.WarehouseEntity.Status.Value(req.Status),             // 状态0表示正常,1表示禁用
		dao.WarehouseEntity.IsDefault.Value(req.IsDefault),       // 是否为默认仓库,0表示不是，1表示是
		dao.WarehouseEntity.PersonID.Value(req.PersonID),         // 仓库负责人,关联到enterprise_person表主键id
		dao.WarehouseEntity.Describe.Value(req.Describe),         // 仓库名称描述
	); err != nil {
		return errors.New("创建失败")
	}
	return nil
}

func (s _warehouseService) ModifyDefaultByIdApi(ctx context.Context, enterpriseId int64, id int64) error {
	if _, err := dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.EnterpriseID.Eq(enterpriseId)).UpdateSimple(
		dao.WarehouseEntity.IsDefault.Value(0), // 是否为默认仓库,0表示不是，1表示是
	); err != nil {
		return errors.New("创建失败")
	}
	if _, err := dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.ID.Eq(id), dao.WarehouseEntity.EnterpriseID.Eq(enterpriseId)).UpdateSimple(
		dao.WarehouseEntity.IsDefault.Value(1), // 是否为默认仓库,0表示不是，1表示是
	); err != nil {
		return errors.New("创建失败")
	}
	return nil
}

func (s _warehouseService) GetWarehouseByIdApi(ctx context.Context, enterpriseId int64, id int64) (*model.WarehouseEntity, error) {
	return dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.EnterpriseID.Eq(enterpriseId), dao.WarehouseEntity.ID.Eq(id)).First()
}

func (s _warehouseService) GetWarehouseByEnterpriseIdApi(ctx context.Context, enterpriseId int64) (*model.WarehouseEntity, error) {
	return dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.EnterpriseID.Eq(enterpriseId)).First()
}

func (s _warehouseService) GetWarehouseByEnterpriseDefaultIdApi(ctx context.Context, enterpriseId int64) (*model.WarehouseEntity, error) {
	return dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.EnterpriseID.Eq(enterpriseId), dao.WarehouseEntity.IsDefault.Eq(1)).First()
}

func (s _warehouseService) GetWarehouseByIdListApi(ctx context.Context, idList []int64) ([]*model.WarehouseEntity, error) {
	return dao.WarehouseEntity.WithContext(ctx).Where(dao.WarehouseEntity.ID.In(idList...)).Find()
}

var WarehouseService = new(_warehouseService)
